/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica;

/**
 *
 * @author joshi
 */
import java.awt.Color;
import java.awt.Font;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;

import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Year;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;


public class series extends JFrame {

    private static String titulo="";
    private static String labelx="";
    private static  String labely=" ";
    private static int num = 0;

   static Object[][] data= null;
   private static int tamano=0;



    public series(String titulo, String labelx, String labely, Object[][] datos){
 
        
        this.titulo = titulo;
        this.labelx = labelx;
        this.labely = labely;
        this.data = datos;
        tamano();
        
        XYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setMouseZoomable(true, false);
        setContentPane(chartPanel);

    }

    public series(String titulo, String labelx, String labely, int num , Object [][] datos){
        
        this.data = datos;
        this.titulo = titulo;
        this.labelx = labelx;
        this.labely = labely;
        this.num = num;
        tamano();
        XYDataset dataset = null;
        if (num ==2){
            dataset = createDataset();
        }
        else if (num==3){
            dataset = createDataset3();
        }
        else if (num== 4){
            dataset = createDataset4();
        }
        else{
            dataset = createDataset();
        }

        
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new java.awt.Dimension(300, 270));
        chartPanel.setMouseZoomable(true, false);
        setContentPane(chartPanel);

    }

    

    /**
     * Creates a chart.
     *
     * @param dataset  a dataset.
     *
     * @return A chart.
     */
    private static JFreeChart createChart(XYDataset dataset) {

        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            titulo,  // title
            labelx,             // x-axis label
            labely,   // y-axis label
            dataset,            // data
            false,               // create legend?
            true,               // generate tooltips?
            false               // generate URLs?
        );

        chart.setBackgroundPaint(Color.white);

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);


       
       

        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));

        return chart;

    }

    /**
     * Creates a dataset, consisting of two series of monthly data.
     *
     * @return The dataset.
     */
    private static XYDataset createDataset() {


   // Create a time series chart
        int d = 0;
        int m = 0;
        int a = 0;
        String vec[] =null;
        String aux = "";
        String fecha="", fechant="";
        TimeSeries s1 = new TimeSeries(labely, Day.class);


        
        for (int i=0; i<tamano  ; i++){
             if(data[i][0]!=null){
                fecha = data[i][2].toString();

                if (fecha.compareTo(fechant)!=0){ //chapus
                vec = fecha.split(",");
                d = java.lang.Integer.valueOf(vec[0]);
                m = java.lang.Integer.valueOf(vec[1]);
                a = java.lang.Integer.valueOf(vec[2]);
                aux = data[i][3].toString();
                fechant = fecha;
                if (num == 2){ //es que son datos de administrador
                s1.add(new Day(d,m,a), java.lang.Integer.valueOf(aux)*0.0001);
                }
                 else{

                    s1.add(new Day(d,m,a), java.lang.Integer.valueOf(aux));

                }
                }
            }
            else
            {
                break;
            }
        }
        

        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);


        dataset.setDomainIsPointsInTime(true);

        return dataset;

    }


      private static XYDataset createDataset2() {
   // Create a time series chart

        int d = 0;
        int m = 0;
        int a = 0;
        String vec[] =null;
        String fecha="", fechant="";

        String aux = "";
        TimeSeries s1 = new TimeSeries(labely, Day.class);

        
        for (int i=0; i<tamano  ; i++){

            if (data[i][0]!=null){
                fecha = data[i][2].toString();

                if (fecha.compareTo(fechant)!=0){
                vec = fecha.split(",");
                d = java.lang.Integer.valueOf(vec[0]);
                m = java.lang.Integer.valueOf(vec[1]);
                a = java.lang.Integer.valueOf(vec[2]);
                aux = data[i][1].toString();
                s1.add(new Day(d,m,a), java.lang.Integer.valueOf(aux));
                fechant = fecha;
                }
            }
            else{
                break;
            }

        
        }
        



        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);


        dataset.setDomainIsPointsInTime(true);

        return dataset;

    }




      private static XYDataset createDataset3() {
     // Create a time series chart

        int m = 0;
        int a = 0;
        
        TimeSeries s1 = new TimeSeries(labely, Month.class);

        for (int i=0; i< pronostico1.mes.length  ; i++){
           
            if (pronostico1.mes[i][0]!=0){
            m = java.lang.Integer.valueOf(pronostico1.mes[i][0]);
            a = java.lang.Integer.valueOf(pronostico1.mes[i][1]);
            s1.add(new Month(m, a),pronostico1.mes[i][2]*0.0001);

                try{
                if (i>0){
                    pronostico1.ymes = pronostico1.mes[i][2];
                    pronostico1.ytmes = pronostico1.mes[i-1][2];
                }
                }
                catch(Exception e){

                }

            }
            else{
                break;
            }


        }
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);

        dataset.setDomainIsPointsInTime(true);

        return dataset;
    }


       private static XYDataset createDataset4() {
     // Create a time series chart


        int a = 0;

        TimeSeries s1 = new TimeSeries(labely, Year.class);

        for (int i=0; i< pronostico1.year.length  ; i++){
          

            if (pronostico1.year[i][0]!=0){
            a = java.lang.Integer.valueOf(pronostico1.year[i][0]);
            s1.add(new Year(a),pronostico1.year[i][1]*0.0001);

                try{
                if (i>0){
                    pronostico1.yyear = pronostico1.year[i][1];
                    pronostico1.ytyear = pronostico1.year[i-1][1];
                }
                }
                catch(Exception e){

                }

            }
            else{
                break;
            }

        }
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);

        dataset.setDomainIsPointsInTime(true);

        return dataset;
    }



    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     *
     * @return A panel.
     */
    public static JPanel createPanel() {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }

    public static JPanel createPanel2() {
        JFreeChart chart = createChart(createDataset2());
        return new ChartPanel(chart);
    }

    public static JPanel createPanel3() {
        JFreeChart chart = createChart(createDataset3());
        return new ChartPanel(chart);
    }

    public static JPanel createPanel4() {
        JFreeChart chart = createChart(createDataset4());
        return new ChartPanel(chart);
    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {

//        series demo = new series("Serie de tiempo ganacia","Fecha","Ganancia");
//        demo.pack();
//        RefineryUtilities.centerFrameOnScreen(demo);
//        demo.setVisible(true);

    }

    private void tamano() {

        for(int i=0; i<data.length;i++){
            if (data[i][0]==null){
                break;
            }
            tamano++;
        }
        System.out.println(tamano);
    }



}
